<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 频道列表 -->
    <!--
通过v-model绑定当前激活标签对应的索引值，默认情况下启用第一个标签
通过animated属性可以开启切换标签内容时的转场动画
通过swipeable属性可以开启滑动切换标签页
-->
      <van-tabs class="channel-tabs"
      v-model="active" animated swipeable>
          <van-tab
          v-for="item in channels"
          :key="item.id"
          :title="item.name">
              <article-list :channel="item"></article-list>
          </van-tab>
          <div slot="nav-right" class="placeholder">
          </div>
          <div slot="nav-right" class="hamburger-btn">
              <i @click="isEditChannelShow = true" class="iconfont icon-gengduo"></i>
          </div>
      </van-tabs>
      <!-- 频道编辑 -->
      <van-popup
        class="edit-channel-popup"
        v-model="isEditChannelShow"
        position="bottom"
        :style="{ height: '100%' }"
        closeable
        close-icon-position="top-left">
        <!-- 传递channels 传递active高亮索引 定义update-active事件-->
        <channel-edit @update-active="onUpdateActive" :myChannels="channels" :active="active"  />
        </van-popup>
      <!-- /频道编辑 -->
  </div>
</template>

<script>
// 1. 导入 获取频道列表的方法
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit'
export default {
  name: 'MYhome',
  data () {
    return {
      active: 1,
      // 4. 定义数据接收频道列表
      channels: [],
      isEditChannelShow: false // 这里我们先设置为 true 就能看到弹窗的页面了
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  created () {
    // 3. 调用获取频道列表
    this.loadChannels()
  },
  methods: {
    onUpdateActive (index) {
      this.active = index
      this.isEditChannelShow = false
    },
    // 2. 定义加载频道列表数据的方法
    async loadChannels () {
      try {
        const { data: res } = await getUserChannels()
        console.log('频道列表的数据', res)
        this.channels = res.data.channels
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    }
  }
}
</script>

<style scoped lang="less">

.home-container {
  padding-top: 180px;
  padding-bottom: 100px;
  /deep/.van-nav-bar__title {
    max-width: unset;
  }
  .edit-channel-popup {
  padding-top: 100px;
  box-sizing: border-box;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
}
/deep/ .channel-tabs {
  .placeholder {
  flex-shrink: 0;
  width: 66px;
  height: 82px;
}

.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;
  i.iconfont {
    font-size: 33px;
  }
  &:before {
    content: "";
    position: absolute;
    left: 0;
    width: 1px;
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}
.van-tab {
  border-right: 1px solid #EDEFF3;
  min-width: 200px;
  height: 80px;
  .van-tab__text {
    font-size: 27px;
    color: #777777;
  }
}

.van-tab--active .van-tab__text{
  color: #333 !important;
}
.van-tabs__line {
  width:31px !important;
  height:6px;
  background:rgba(50,150,250,1);
  border-radius:3px;
  bottom: 8px;
}
.van-tabs__nav {
  padding: 0;
}
.van-tabs__wrap {
  position: fixed;
  top: 92px;
  z-index: 2;
  width: 100%;
}

}
</style>
